package com.bocai.grainshop.product.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.bocai.grainshop.product.entity.AttrAttrgroupRelationEntity;
import com.bocai.grainshop.product.entity.AttrEntity;
import com.bocai.grainshop.product.service.AttrAttrgroupRelationService;
import com.bocai.grainshop.product.service.AttrService;
import com.bocai.grainshop.product.service.CategoryService;
import com.bocai.grainshop.product.vo.AttrGroupRelationVo;
import com.bocai.grainshop.product.vo.AttrGroupWithAttrsVo;
import com.bocai.grainshop.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bocai.grainshop.product.entity.AttrGroupEntity;
import com.bocai.grainshop.product.service.AttrGroupService;
import com.bocai.common.utils.PageUtils;
import com.bocai.common.utils.R;



/**
 * 属性分组
 *
 * @author SunJinBo
 * @email 2374461077@qq.com
 * @date 2022-03-01 17:47:31
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    /**
     * 获取当前分类下的所有属性分组以及所有分组下的所有属性   前台录入规格参数页面
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttr(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> attrsVos = attrGroupService.getAttrGroupWithAttrByCatelogId(catelogId);
        return R.ok().put("data",attrsVos);
    }

    /**
     * 获取关联列表
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", entities);
    }

    /**
     * 新增关联
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrAttrgroupRelationEntity> vos) {

        relationService.saveBatchSort(vos);
        return R.ok();
    }


    /**
     * 获取新增列表
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getNoRelationAttr(params, attrgroupId);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catId") Long groupId){
        PageUtils page = attrGroupService.queryPage(params,groupId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();

        Long[] catelogPath = categoryService.findCatelogPath(catelogId);

        attrGroup.setCatelogPath(catelogPath);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroupEntity){
		attrGroupService.save(attrGroupEntity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 关联移除
     */
    @PostMapping("/attr/relation/delete")
    public R relationRemove(@RequestBody List<AttrAttrgroupRelationEntity> vos){
        relationService.removeByMoreIds(vos);
        return R.ok();
    }

}
